package packml;

import java.awt.Color;
import java.rmi.RemoteException;

import controller.acquaintance.*;
import controller.subsystemcom.OEE;
import controller.subsystemcom.PackML;


public class Packmlhandler extends PackmlUI implements Runnable{

	public static volatile int receiveData;
	public I_MesLogs packMLWorkCellController = PackML.getInstance();
	public I_WorkCellController_PackmlHandler packmlhandlerWorkCellController = PackML.getInstance();
	private static final long serialVersionUID = 1L;
	public I_WorkCellController_OEE packmlhandlerOEE = OEE.getInstance();
	private String stateComplete;
	private String lastState = "";
	
	


	public void run(){
		System.out.println("packmlhandler");
		try {
			System.out.println("get new order");
			packmlhandlerWorkCellController.fetchNewOrder();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(true){
			try {
				stateMachine();
			} catch (RemoteException e)
			{
				e.printStackTrace();
			}
		}
	}
	public synchronized void delay(int i) {
		try {
			wait(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}



	public void stateMachine() throws RemoteException{
		if(brick=="Available" && unsuspend==1)
			unsuspending();
		
		if(receiveData==5){
			//state="Complete";
			receiveData = 0;
			complete();
			//stateMachine();
		}

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

		if(state1=="Resetting"){
			state1 = "null";
			resetting();
		}
		if(state1=="Holding"){
			state1 = "null";
			holding();
		}
		else if(state1=="Stopping"){
			state1 = "null";

			stopping();
		}

		else if(state=="aborted"){
			aborted();
		}

		else if(state=="idle"){
			idle();
		}

		else if(state=="suspending"){
			suspending();
		}

		else if(state=="stopped"){
			stopped();
		}

		else if(state=="suspended"){
			suspended();
		}
		else if(state=="completing"){
			completing();
		}

		else if(state=="held"){
			held();
		}
		else if(state=="ebrake"){
			aborting();
		}

		else if(state=="Stopping"){
			stopping();
		}

		else if(state=="Resetting" ){

			resetting();
		}

		else if(state=="Starting"){
			starting();
		}

		else if(state=="Holding"){
			holding();
		}

		else if(state=="Unholding"){
			unholding();
		}

		else if(state=="Execute"){
			
			execute();
		}

		else if(state=="Complete"){
			complete();
		}

		else if(state=="Un-Suspending"){
			unsuspending();
		}

		else if(state=="Clearing"){
			clearing();
		}
	}


	public void stopping() throws RemoteException{

		packmlhandlerWorkCellController.PML_state_Stopping(System.currentTimeMillis(), "");
state="Stopping";

		completing=0;


		resetCurrentTimer=1;
		totalTimerExecute.stop();


		jButton1.setEnabled(false);
		jButton2.setEnabled(false);
		jButton3.setEnabled(false);
		jButton4.setEnabled(false);
		jButton6.setEnabled(false);
		jButton7.setEnabled(false);
		jButton8.setEnabled(false);

		jTextField5.setBackground(Color.green);

		jTextField2.setBackground(Color.white);
		jTextField3.setBackground(Color.white);
		jTextField4.setBackground(Color.white);
		jTextField6.setBackground(Color.white);
		jTextField7.setBackground(Color.white);
		jTextField8.setBackground(Color.white);
		jTextField9.setBackground(Color.white);
		jTextField10.setBackground(Color.white);
		jTextField11.setBackground(Color.white);
		jTextField12.setBackground(Color.white);
		jTextField13.setBackground(Color.white);
		jTextField14.setBackground(Color.white);
		jTextField15.setBackground(Color.white);
		jTextField16.setBackground(Color.white);
		jTextField17.setBackground(Color.white);
		jTextField18.setBackground(Color.white);

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

		else if(receiveData==1){
			receiveData = 0;
			stopped();
		}

	}
	public void stopped() throws RemoteException{

		if (! state.equalsIgnoreCase(lastState))
			packMLWorkCellController.sendLogPML_Stopped(System.currentTimeMillis(),"");
		jTextField5.setBackground(Color.white);
		jTextField6.setBackground(Color.white);
		jTextField3.setBackground(Color.green);
		jButton3.setEnabled(true);
		jButton5.setEnabled(true);

		state = "stopped";
		lastState = state;

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
	}

	public void resetting() throws RemoteException{
		if(stateComplete=="Completed"){
			stateComplete=null;
			packmlhandlerWorkCellController.fetchNewOrder();

		}

		if (! state.equalsIgnoreCase(lastState))
			packmlhandlerWorkCellController.PML_state_Resetting(System.currentTimeMillis(),"");
		totalTimerExecute.start();
		resetTotalTimer=1;
		jButton5.setEnabled(false);
		jTextField7.setBackground(Color.green);
		jTextField3.setBackground(Color.white);
		jTextField17.setBackground(Color.white);
		state="Resetting";
		lastState  = state;

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

		else if(receiveData==6){
			receiveData = 0;
			idle();
		}

	}

	public void idle() throws RemoteException{
		packMLWorkCellController.sendLogPML_Idle(System.currentTimeMillis(),"");

		unhold=0;
		exec=0;
		jButton3.setEnabled(false);
		jButton5.setEnabled(true);
		jTextField7.setBackground(Color.white);
		jTextField8.setBackground(Color.green);
		jButton4.setEnabled(true);
		state="null";
		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
	}

	public void starting() throws RemoteException{
		packmlhandlerWorkCellController.PML_state_Starting(System.currentTimeMillis(),"");

		jTextField11.setBackground(Color.green);
		jTextField8.setBackground(Color.white);
		jButton3.setEnabled(false);
		jButton6.setEnabled(false);
		jButton7.setEnabled(false);
		jButton8.setEnabled(false);
		exec=1;
		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}


	}

	public void execute() throws RemoteException{
//		unsuspend=0;
		//delay(2000);
//		System.out.println("EXECUTE   "+brick);
//		if(unsuspend==0){
		if (! state.equalsIgnoreCase(lastState))
			packMLWorkCellController.sendLogPML_Execute(System.currentTimeMillis(),"");
		//suspendPermission = true; not needed
		//		packMLWorkCellController.sendLogOrder_Start(System.currentTimeMillis(),"");

		state = "Execute";
		lastState=state;
		resetCurrentTimer=0;

		currentTimerExecute.start();
		totalTimerExecute.start();	
		jTextField8.setBackground(Color.white);
		jTextField11.setBackground(Color.white);
		jTextField10.setBackground(Color.white);
		jTextField9.setBackground(Color.white);
		jTextField12.setBackground(Color.green);
		jButton6.setEnabled(true);
		jButton4.setEnabled(false);
		jButton3.setEnabled(false);

		if(order=="Complete"){
			order=null;
			completing();

		}
		if(receiveData==4){
			System.out.println("EBRAKE");
			state="ebrake";
			receiveData = 0;
			stateMachine();

		}
		if(brick=="UnAvailable"){
			brick=null;
			suspending();
		}
		exec=0;

		}
//	}

	public void holding() throws RemoteException{
		packmlhandlerWorkCellController.PML_state_Holding(System.currentTimeMillis(),"");
		resetCurrentTimer=1;
		totalTimerExecute.stop();
		jButton6.setEnabled(false);
		jTextField14.setBackground(Color.green);
		jTextField12.setBackground(Color.white);
		state="Holding";
//System.out.println("Inside holding" +state);
		if(receiveData==1){
			receiveData=0;
			held();
		}
		else if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

	}

	public void held() throws RemoteException{

		packMLWorkCellController.sendLogPML_Held(System.currentTimeMillis(),"");

		jTextField14.setBackground(Color.white);
		jTextField13.setBackground(Color.green);

		jButton4.setEnabled(true);
		unhold = 1;
		state="null";

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
	}

	public void unholding() throws RemoteException{
		packmlhandlerWorkCellController.PML_state_UnHolding(System.currentTimeMillis(),"");
		unhold=0;
		jTextField10.setBackground(Color.green);
		jTextField13.setBackground(Color.white);
		delay(500);

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

		//state="Execute";
		execute();
	}

	public void completing() throws RemoteException{
		packmlhandlerWorkCellController.PML_state_Completing(System.currentTimeMillis(),"");

		resetCurrentTimer=1;
		totalTimerExecute.stop();
		state="null";
		jButton6.setEnabled(false);
		jButton3.setEnabled(false);
		jButton7.setEnabled(false);
		jTextField12.setBackground(Color.white);
		jTextField16.setBackground(Color.green);
		completing=1;



		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

	}

	public void complete() throws RemoteException{

		
		if (! state.equalsIgnoreCase(lastState))
			packMLWorkCellController.sendLogPML_Complete(System.currentTimeMillis(),"");
		stateComplete = "Completed";
		lastState = stateComplete;
		completing=0;
		jButton3.setEnabled(true);
		jTextField17.setBackground(Color.green);
		jTextField16.setBackground(Color.white);

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
	}

	public void aborting() throws RemoteException{
		state="null";
		completing = 0;
		jButton1.setBackground(Color.red);


		resetCurrentTimer=1;
		totalTimerExecute.stop();

		jButton5.setEnabled(true);
		jButton1.setEnabled(true);
		jButton2.setEnabled(false);
		jButton3.setEnabled(false);
		jButton4.setEnabled(false);
		jButton6.setEnabled(false);
		jButton7.setEnabled(false);
		jButton8.setEnabled(false);

		jTextField2.setBackground(Color.green);
		jTextField3.setBackground(Color.white);
		jTextField4.setBackground(Color.white);
		jTextField5.setBackground(Color.white);
		jTextField6.setBackground(Color.white);
		jTextField7.setBackground(Color.white);
		jTextField8.setBackground(Color.white);
		jTextField9.setBackground(Color.white);
		jTextField10.setBackground(Color.white);
		jTextField11.setBackground(Color.white);
		jTextField12.setBackground(Color.white);
		jTextField13.setBackground(Color.white);
		jTextField14.setBackground(Color.white);
		jTextField15.setBackground(Color.white);
		jTextField16.setBackground(Color.white);
		jTextField17.setBackground(Color.white);
		jTextField18.setBackground(Color.white);

		delay(4000);
		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
		else{ 
			aborted();//no change here
			receiveData = 0;	
		}
	}

	public void aborted() throws RemoteException{

		packMLWorkCellController.sendLogPML_Aborted(System.currentTimeMillis(),"");

		jButton7.setEnabled(true);

		jTextField4.setBackground(Color.green);
		jTextField2.setBackground(Color.white);

		state="null";

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}

	}

	public void clearing() throws RemoteException{
		jButton1.setEnabled(false);
		jButton1.setBackground(Color.white);
		jButton7.setEnabled(false);
		jTextField6.setBackground(Color.green);
		jTextField4.setBackground(Color.white);

		delay(500);
		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
		stopped();
	}

	public void suspending() throws RemoteException{
		if (! state.equalsIgnoreCase(lastState))
			packmlhandlerWorkCellController.PML_state_Suspending(System.currentTimeMillis(),"");
		state="suspending";
		lastState=state;
		resetCurrentTimer=1;
		totalTimerExecute.stop();


		jButton6.setEnabled(false);
		jTextField12.setBackground(Color.white);
		jTextField18.setBackground(Color.green);

		state="suspending";
		if(state1=="Stopping")
			state="Stopping";//was null here changing frrom null to stopping solves the problem b/w suspending and stopping 

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}


		else if(receiveData==7){
			receiveData=0;
			suspended();
		}
	}

	public void suspended() throws RemoteException{
if(brick=="UnAvailable"){
	System.out.println("suspended   "+brick);
		packMLWorkCellController.sendLogPML_Suspended(System.currentTimeMillis(),"");
		jTextField18.setBackground(Color.white);
		jTextField15.setBackground(Color.green);

		jTextField18.setBackground(Color.white);
		jTextField15.setBackground(Color.green);

		jButton4.setEnabled(true);
		unsuspend=1;
		state="null";

		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();
		}
		//	if(brick=="Available")
		//		unsuspending();
}

	}

	public void unsuspending() throws RemoteException{

		packmlhandlerWorkCellController.PML_state_UnSuspending(System.currentTimeMillis(),"");
		unsuspend=0;
		jTextField15.setBackground(Color.white);
		jTextField9.setBackground(Color.green);
		jButton5.setEnabled(true);
		jButton4.setEnabled(false);
		delay(1500);
		state="null";


		if(receiveData==4){
			state="ebrake";
			receiveData = 0;
			stateMachine();				
		}
		execute();
	}






}